<template>
  <div>
    <RainfallChartContent :cdata="chartData" />
  </div>
</template>

<script>
import RainfallChartContent from './chart.vue'
import { mapState } from 'vuex'
import axios from 'axios'
export default {
  name: 'RainfallChartContainer',
  components: {
    RainfallChartContent
  },
  data() {

    return {
      chartData: {

        "timeData": [
          "2015-05",
          "2015-06",
          "2015-07",
          "2015-08",
          "2015-09",
          "2015-10",
          "2015-11",
          "2015-12",
          "2016-01",
          "2016-02",
          "2016-03",
          "2016-04",
          "2016-05",
          "2016-06",
          "2016-07",
          "2016-08",
          "2016-09",
          "2016-10",
          "2016-11",
          "2016-12",
          "2017-01",
          "2017-02",
          "2017-03",
          "2017-04",
          "2017-05",
          "2017-06",
          "2017-07",
          "2017-08",
          "2017-09",
          "2017-10",
          "2017-11",
          "2017-12",
          "2018-01",
          "2018-02",
          "2018-03",
          "2018-04",
          "2018-05",
          "2018-06",
          "2018-07",
          "2018-08",
          "2018-09",
          "2018-10",
          "2018-11",
          "2018-12",
          "2019-01",
          "2019-02",
          "2019-03",
          "2019-04",
          "2019-05",
          "2019-06",
          "2019-07",
          "2019-08",
          "2019-09",
          "2019-10",
          "2019-11",
          "2019-12",
          "2020-01",
          "2020-02",
          "2020-03",
          "2020-04",
          "2020-05",
          "2020-06",
          "2020-07",
          "2020-08",
          "2020-09",
          "2020-10",
          "2020-11",
          "2020-12",
          "2021-01",
          "2021-02",
          "2021-03",
          "2021-04",
          "2021-05",
          "2021-06",
          "2021-07",
          "2021-08",
          "2021-09",
          "2021-10",
          "2021-11",
          "2021-12",
          "2022-01",
          "2022-02",
          "2022-03",
          "2022-04",
          "2022-05",
          "2022-06",
          "2022-07",
          "2022-08",
          "2022-09",
          "2022-10",
          "2022-11",
          "2022-12",
          "2023-01",
          "2023-02",
          "2023-03",
          "2023-04",
          "2023-05",
          "2023-06",
          "2023-07",
          "2023-08",
          "2023-09",
          "2023-10",
          "2023-11",
          "2023-12",
          "2024-01",
          "2024-02",
          "2024-03",
          "2024-04",
          "2024-05",
          "2024-06",
          "2024-07",
          "2024-08",
          "2024-09",
          "2024-10"
        ],
        "activityData": [
          2.58,
          22.89,
          89.13,
          153.18,
          166.45,
          144.97,
          198.6,
          238.35,
          240.73,
          177.36,
          296.52,
          351.31,
          490.5,
          421.61,
          468.33,
          477.58,
          462.2,
          515.57,
          579.72,
          575.07,
          488.56,
          575.33,
          723.64,
          604.94,
          594.26,
          623.35,
          610.22,
          664.32,
          623.38,
          531.66,
          533.4,
          710.52,
          692.91,
          479.22,
          745.09,
          698.74,
          724.54,
          650.25,
          718.22,
          756.16,
          796.08,
          699.6,
          722.31,
          2037.09,
          935.2,
          725.12,
          1022.32,
          952.81,
          894.32,
          800.77,
          1111.47,
          958.91,
          802.37,
          781.42,
          870.85,
          826.6,
          872.22,
          786.53,
          1470.39,
          1436.59,
          1193.3,
          1102.24,
          1035.29,
          959.53,
          884.29,
          908.1,
          1036.62,
          938.23,
          833.94,
          639.52,
          770.6,
          705.94,
          670.16,
          689.8,
          702.63,
          575.61,
          633.73,
          643,
          656.13,
          704.19,
          616.38,
          516.83,
          772.62,
          764.24,
          681.33,
          674.43,
          700.83,
          739.16,
          657.34,
          636.83,
          1016.71,
          955.87,
          696.16,
          739.82,
          836.68,
          706.54,
          844.7,
          696.1,
          766.26,
          834.4,
          796.43,
          666.27,
          740.91,
          654.27,
          690.33,
          545.02,
          759.3,
          737.15,
          682.74,
          733.27,
          723.84,
          714.47,
          647.16,
          561.09
        ],
        "openrankData": [
          2.23,
          6.73,
          14.17,
          28.48,
          41.23,
          39.98,
          49.1,
          59.5,
          65.46,
          63.68,
          71.21,
          89.98,
          113.82,
          119.37,
          130.67,
          136.28,
          138.95,
          156.26,
          169.23,
          177.39,
          167.36,
          179.05,
          193.49,
          182.81,
          178.27,
          191.03,
          172.34,
          175.8,
          161.97,
          150.48,
          147.81,
          180.99,
          187.04,
          159.49,
          169.28,
          175.92,
          183.76,
          170.8,
          177.01,
          172.15,
          197.31,
          197.12,
          186.24,
          308.23,
          268.91,
          230.22,
          242.77,
          248.65,
          231.27,
          221.96,
          263.5,
          246.56,
          213.48,
          193.14,
          187.85,
          213.25,
          216.91,
          211.16,
          297.89,
          341.59,
          329.37,
          326.88,
          295.66,
          258.66,
          242.8,
          240.61,
          244.6,
          241.35,
          228.24,
          201.19,
          198.5,
          190.36,
          176.59,
          186.91,
          182.54,
          167.71,
          164.87,
          147,
          126.4,
          134.95,
          135.55,
          121.1,
          153.07,
          161.49,
          165.49,
          160.05,
          162.37,
          170.33,
          163.86,
          162.53,
          220.46,
          240.32,
          211.69,
          201.3,
          194.3,
          192.88,
          213.6,
          206.35,
          200.45,
          204.07,
          209.16,
          188.14,
          184.51,
          162.5,
          168.2,
          159.73,
          175.2,
          182.86,
          181.58,
          185.25,
          173.76,
          160.01,
          157.06,
          139.57
        ]

      },
    };
  },
  mounted() {
  },
  computed: {
    ...mapState(['currentRepository'])
  },
  methods: {
    async fetchData(path) {
      const datePattern = /^\d{4}-(0[1-9]|1[0-2])$/;
      const filterData = (datas) => {
        let filteredDatas = {};
        for (const key in datas) {
          if (datePattern.test(key)) {
            filteredDatas[key] = datas[key];
          }
        }
        return filteredDatas;
      }

      let activityResponse = await axios.get(path + '/activity.json');
      let activityData = await activityResponse.data;
      activityData = filterData(activityData);
      let openrankResponse = await axios.get(path + '/openrank.json');
      let openrankData = await openrankResponse.data;
      openrankData = filterData(openrankData);

      // 获取所有的键
      let activityKeys = Object.keys(activityData);
      let openrankKeys = Object.keys(openrankData);

      // 找出公共的键
      let commonKeys = activityKeys.filter(value => openrankKeys.includes(value));

      // 创建新的json对象
      let data = {
        timeData: commonKeys.map(key => key),
        activityData: commonKeys.map(key => activityData[key]),
        openrankData: commonKeys.map(key => openrankData[key]),
      }
      return data
    }
  },
  watch: {
    currentRepository: {
      handler: async function (newVal) {
        this.chartData = await this.fetchData('https://oss.x-lab.info/open_digger/github/' + newVal);
      },
      deep: true
    }
  }


}
</script>